import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
// import 'package:fluttertoast/fluttertoast.dart';
import '../../util/Toast.dart';
import 'package:flutter_aygsh/http/HttpUtil.dart';
import 'package:flutter_aygsh/http/Api.dart';
import '../../util/AppSaveInfoUtil.dart';
import '../../util/LoadingUtil.dart';
import '../../base/Constant.dart';
import 'LocalNavWebWidget.dart';
import 'OrderComment.dart';
import 'OrderPay.dart';
import '../../util/SignUtil.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import '../../liz_mine/Invoice/chooseInvoice.dart';
import '../../entity/user_receipt_entity.dart';

class OrderDetailPage extends StatefulWidget {
  String id;
  OrderDetailPage({Key key, @required this.id}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return OrderDetailView();
  }
}

class OrderDetailView extends State<OrderDetailPage> {
  EasyRefreshController _controller = new EasyRefreshController();
  String token = "",
      orderNum = "",
      order_id = "",
      postage = '0',
      title = "",
      status = "1",
      address = "",
      create_time = "",
      service_time = "",
      youhui = '0',
      phone = "",
      consignee = '',
      discount_price = "",
      pay_price = "0",
      type = "1",
      discount = "0",
      payment_method = "",
      service_finish_time = "",
      status_zh = '',
      remark = "";

  List detail = new List();
  //发票
  String receipt_content = '';
  String type_zh = '';
  String receipt_name = '';
  String invoice_id;
  int invoice = 0; //是否可以开票
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getString(TOKEN).then((value) {
      token = value;
      getDetail();
      getInvoiceMsg();
    });
  }

  getDetail() async {
    Map map = new Map();
    map['token'] = token;
    map['order_id'] = widget.id;
    map['sign_type'] = createSignType(Api.URL_ORDER_MSG);
    Response<String> response =
        await HttpUtil().post(Api.URL_ORDER_MSG, data: getSign(map));
    Map userMap = json.decode(response.data);
    print(userMap);
    int code = userMap["code"];
    if (code == 1 && mounted) {
      setState(() {
        status_zh = userMap['data']['orderMsg']['status_zh'];
        consignee = userMap['data']['orderMsg']['consignee'];
        phone = userMap['data']['orderMsg']['contact_number'];
        address = userMap['data']['orderMsg']['address'];
        orderNum = userMap['data']['orderMsg']['order_num'];
        title = userMap['data']['orderMsg']['title'];
        status = userMap['data']['orderMsg']['status'].toString();
        discount = userMap['data']['orderMsg']['discount'].toString();
        pay_price = userMap['data']['orderMsg']['pay_price'].toString();
        discount_price = '';
        youhui = userMap['data']['orderMsg']['discount']
            .toString()
            .replaceAll("null", "0");
        postage = userMap['data']['orderMsg']['postage'].toString();
        type = userMap['data']['orderMsg']['type'];
        order_id = userMap['data']['orderMsg']['id'].toString();
        remark = userMap['data']['orderMsg']['remark'];

        service_finish_time = '';
        payment_method = userMap['data']['orderMsg']['payment_method'] == null
            ? '线上'
            : userMap['data']['orderMsg']['payment_method'];
        create_time = userMap['data']['orderMsg']['create_time'];
        service_time = '2020-03-06 17:24:46';

        detail = userMap['data']['orderMsg']['detail'];
        print(userMap['data']['orderMsg']['detail']);
      });
    } else {}
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return new Scaffold(
        appBar: new AppBar(
          flexibleSpace: Container(
            decoration: BoxDecoration(
              color: Color.fromARGB(255, 255, 140, 16),
            ),
          ),
          title: new Text(
            "订单详情",
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.white),
          ),
          elevation: 0,
          backgroundColor: mainColor,
          brightness: Brightness.light,
          centerTitle: true,
          iconTheme: new IconThemeData(color: Colors.white),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
                child: Container(
                    color: f0Color,
                    child: CustomScrollView(shrinkWrap: true, slivers: <Widget>[
                      SliverToBoxAdapter(
                        child: Container(
                          width: double.infinity,
                          child: new Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Container(
                                decoration: BoxDecoration(
                                  color: Color.fromARGB(255, 255, 140, 16),
                                ),
                                // margin: EdgeInsets.all(15.0),
                                padding: EdgeInsets.all(20.0),
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                      child: Text(status_zh,
                                          style: TextStyle(
                                              color: Colors.white,
                                              fontWeight: FontWeight.w500,
                                              fontSize: ScreenUtil.getInstance()
                                                  .setSp(33))),
                                    )
                                  ],
                                ),
                              ),
                              Container(
                                  decoration: BoxDecoration(
                                      color: white,
                                      borderRadius: BorderRadius.circular(
                                          ScreenUtil.getInstance()
                                              .setHeight(DP_15))),
                                  width:
                                      ScreenUtil.getInstance().setWidth(750) -
                                          30,
                                  margin: EdgeInsets.fromLTRB(15, 10, 15, 5),
                                  padding: EdgeInsets.fromLTRB(10, 10, 10, 20),
                                  child: Row(
                                    children: <Widget>[
                                      Image.asset(
                                          'images/address_location.png'),
                                      Container(
                                        width: 10,
                                      ),
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: <Widget>[
                                            Container(
                                              padding: EdgeInsets.fromLTRB(
                                                  0, 8, 0, 0),
                                              child: Text(
                                                  consignee + ' ' + phone,
                                                  style: TextStyle(
                                                      color: black,
                                                      fontSize: ScreenUtil
                                                              .getInstance()
                                                          .setSp(
                                                              NORMAL_FONT_SIZE -
                                                                  2),
                                                      fontWeight:
                                                          FontWeight.w500)),
                                            ),
                                            Container(
                                              height: 8,
                                            ),
                                            Text(address,
                                                style: TextStyle(
                                                  color: black,
                                                  fontSize:
                                                      ScreenUtil.getInstance()
                                                          .setSp(
                                                              NORMAL_FONT_SIZE -
                                                                  3),
                                                )),
                                          ],
                                        ),
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        ),
                      ),
                      SliverList(
                        delegate: SliverChildBuilderDelegate(
                          (context, index) {
                            if (detail.length > 0) {
                              return _renderRow(context, index);
                            } else {
                              return Visibility(
                                visible: true,
                                child: Container(width: 0, height: 0),
                              );
                            }
                          },
                          childCount: detail.length,
                        ),
                      ),
                      SliverToBoxAdapter(
                          child: Container(
                              decoration: BoxDecoration(
                                  color: white,
                                  borderRadius: BorderRadius.circular(
                                      ScreenUtil.getInstance()
                                          .setHeight(DP_15))),
                              width:
                                  ScreenUtil.getInstance().setWidth(750) - 30,
                              margin: EdgeInsets.fromLTRB(15, 10, 15, 5),
                              padding: EdgeInsets.fromLTRB(10, 10, 10, 20),
                              child: Column(
                                children: <Widget>[
                                  Row(
                                    children: <Widget>[
                                      Expanded(
                                        child: Text('商品总额',
                                            style: TextStyle(
                                                color: black,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(
                                                            SMALL_FONT_SIZE))),
                                      ),
                                      Text(
                                        MONEY +
                                            (double.parse(pay_price) +
                                                    double.parse(youhui) -
                                                    double.parse(postage))
                                                .toString(),
                                        style: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(SMALL_FONT_SIZE),
                                          color: black,
                                        ),
                                      ),
                                    ],
                                  ),
                                  Container(
                                    width:
                                        ScreenUtil.getInstance().setWidth(750),
                                    height: 10,
                                  ),
                                  Row(
                                    children: <Widget>[
                                      Expanded(
                                        child: Text('优惠券',
                                            style: TextStyle(
                                                color: black,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(
                                                            SMALL_FONT_SIZE))),
                                      ),
                                      Text(
                                        MONEY + youhui,
                                        style: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(SMALL_FONT_SIZE),
                                          color: black,
                                        ),
                                      ),
                                    ],
                                  ),
                                  Container(
                                    width:
                                        ScreenUtil.getInstance().setWidth(750),
                                    height: kMargin20,
                                  ),
                                  Row(
                                    children: <Widget>[
                                      Expanded(
                                        child: Text('运费',
                                            style: TextStyle(
                                                color: black,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(
                                                            SMALL_FONT_SIZE))),
                                      ),
                                      Text(
                                        MONEY + postage,
                                        style: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(SMALL_FONT_SIZE),
                                          color: black,
                                        ),
                                      ),
                                    ],
                                  ),
                                  Container(
                                    width:
                                        ScreenUtil.getInstance().setWidth(750),
                                    height: kMargin20,
                                  ),
                                  // Row(
                                  //   children: <Widget>[
                                  //     Expanded(
                                  //       child: Text('税费',
                                  //           style: TextStyle(
                                  //               color: black,
                                  //               fontSize:
                                  //                   ScreenUtil.getInstance()
                                  //                       .setSp(
                                  //                           SMALL_FONT_SIZE))),
                                  //     ),
                                  //     Text(
                                  //       '¥0.00',
                                  //       style: TextStyle(
                                  //         fontSize: ScreenUtil.getInstance()
                                  //             .setSp(SMALL_FONT_SIZE),
                                  //         color: black,
                                  //       ),
                                  //     ),
                                  //   ],
                                  // ),
                                  Container(
                                    width:
                                        ScreenUtil.getInstance().setWidth(750),
                                    height: 10,
                                  ),
                                  Row(
                                    children: <Widget>[
                                      Expanded(
                                        child: Text('实付金额',
                                            style: TextStyle(
                                                color: black,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(
                                                            NORMAL_FONT_SIZE))),
                                      ),
                                      Text(
                                        MONEY + pay_price,
                                        style: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(NORMAL_FONT_SIZE),
                                          color: Colors.red,
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ))),
                      SliverToBoxAdapter(
                          child: Container(
                              decoration: BoxDecoration(
                                  color: white,
                                  borderRadius: BorderRadius.circular(
                                      ScreenUtil.getInstance()
                                          .setHeight(DP_15))),
                              width:
                                  ScreenUtil.getInstance().setWidth(750) - 30,
                              margin: EdgeInsets.fromLTRB(15, 10, 15, 5),
                              padding: EdgeInsets.fromLTRB(10, 10, 10, 20),
                              child: Column(
                                children: <Widget>[
                                  Row(
                                    children: <Widget>[
                                      Expanded(
                                        child: Text('发票类型',
                                            style: TextStyle(
                                                color: black,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(
                                                            SMALL_FONT_SIZE))),
                                      ),
                                      Text(
                                        type_zh,
                                        style: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(SMALL_FONT_SIZE),
                                          color: black,
                                        ),
                                      ),
                                    ],
                                  ),
                                  Container(
                                    width:
                                        ScreenUtil.getInstance().setWidth(750),
                                    height: kMargin20,
                                  ),
                                  Row(
                                    children: <Widget>[
                                      Expanded(
                                        child: Text('发票抬头',
                                            style: TextStyle(
                                                color: black,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(
                                                            SMALL_FONT_SIZE))),
                                      ),
                                      GestureDetector(
                                        onTap: () {},
                                        child: Text(
                                          receipt_name,
                                          style: TextStyle(
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(SMALL_FONT_SIZE),
                                            color: black,
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                  // Row(
                                  //   children: <Widget>[
                                  //     Expanded(
                                  //       child: Text('发票内容',
                                  //           style: TextStyle(
                                  //               color: black,
                                  //               fontSize:
                                  //                   ScreenUtil.getInstance()
                                  //                       .setSp(
                                  //                           SMALL_FONT_SIZE))),
                                  //     ),
                                  //     Text(
                                  //       receipt_content,
                                  //       style: TextStyle(
                                  //         fontSize: ScreenUtil.getInstance()
                                  //             .setSp(SMALL_FONT_SIZE),
                                  //         color: black,
                                  //       ),
                                  //     ),
                                  //   ],
                                  // ),
                                ],
                              ))),
                      SliverToBoxAdapter(
                          child: Container(
                              decoration: BoxDecoration(
                                  color: white,
                                  borderRadius: BorderRadius.circular(
                                      ScreenUtil.getInstance()
                                          .setHeight(DP_15))),
                              width:
                                  ScreenUtil.getInstance().setWidth(750) - 30,
                              margin: EdgeInsets.fromLTRB(15, 10, 15, 5),
                              padding: EdgeInsets.fromLTRB(10, 10, 10, 20),
                              child: Column(
                                children: <Widget>[
                                  Row(
                                    children: <Widget>[
                                      Expanded(
                                        child: Text('订单编号',
                                            style: TextStyle(
                                                color: black,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(
                                                            SMALL_FONT_SIZE))),
                                      ),
                                      Text(
                                        orderNum,
                                        style: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(SMALL_FONT_SIZE),
                                          color: black,
                                        ),
                                      ),
                                    ],
                                  ),
                                  Container(
                                    width:
                                        ScreenUtil.getInstance().setWidth(750),
                                    height: kMargin20,
                                  ),
                                  Row(
                                    children: <Widget>[
                                      Expanded(
                                        child: Text('下单时间',
                                            style: TextStyle(
                                                color: black,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(
                                                            SMALL_FONT_SIZE))),
                                      ),
                                      Text(
                                        create_time,
                                        style: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(SMALL_FONT_SIZE),
                                          color: black,
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ))),
                    ]))),
            // Expanded(
            //   child:
            Container(
              alignment: Alignment.topRight,
              margin: EdgeInsets.fromLTRB(0, 15, 0, 0),
              color: white,
              padding: EdgeInsets.all(15.0),
              width: double.infinity,
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(''),
                  ),
                  Offstage(
                    offstage: status != "1",
                    child: new GestureDetector(
                      onTap: () {
                        showDialog(
                          context: context,
                          builder: (context) => new AlertDialog(
                            title: new Text('提示'),
                            content: new Text('确定取消该订单吗，取消后不可撤回'),
                            actions: <Widget>[
                              new FlatButton(
                                onPressed: () =>
                                    Navigator.of(context).pop(false),
                                child: new Text(
                                  '取消',
                                  style: TextStyle(color: Colors.grey),
                                ),
                              ),
                              new FlatButton(
                                onPressed: () {
                                  Navigator.of(context).pop(false);
                                  showDialog(
                                      context: context,
                                      barrierDismissible: false,
                                      builder: (_) {
                                        return new NetLoadingDialog(
                                          loadingText: "取消中...",
                                          bgColor: Color.fromARGB(255, 0, 0, 0),
                                          requestCallBack: cancle(),
                                          outsideDismiss: false,
                                        );
                                      });
                                },
                                child: new Text('确定取消',
                                    style: TextStyle(color: mainColor)),
                              ),
                            ],
                          ),
                        );
                      },
                      child: new Container(
                        padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                        decoration: new BoxDecoration(
                            borderRadius: BorderRadius.circular(10.0),
                            border:
                                Border.all(color: Colors.black54, width: 1.0)),
                        child: Text(
                          '取消订单',
                          style:
                              TextStyle(fontSize: 14.0, color: Colors.black54),
                        ),
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: type != "2",
                    child: new GestureDetector(
                      onTap: () {
                        showDialog(
                          context: context,
                          builder: (context) => new AlertDialog(
                            title: new Text('提示'),
                            content: new Text('确定取消该订单吗，取消后不可撤回'),
                            actions: <Widget>[
                              new FlatButton(
                                onPressed: () =>
                                    Navigator.of(context).pop(false),
                                child: new Text(
                                  '取消',
                                  style: TextStyle(color: Colors.grey),
                                ),
                              ),
                              new FlatButton(
                                onPressed: () {
                                  Navigator.of(context).pop(false);
                                  showDialog(
                                      context: context,
                                      barrierDismissible: false,
                                      builder: (_) {
                                        return new NetLoadingDialog(
                                          loadingText: "取消中...",
                                          bgColor: Color.fromARGB(255, 0, 0, 0),
                                          requestCallBack: cancle(),
                                          outsideDismiss: false,
                                        );
                                      });
                                },
                                child: new Text('确定取消',
                                    style: TextStyle(color: mainColor)),
                              ),
                            ],
                          ),
                        );
                      },
                      child: new Container(
                        padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                        decoration: new BoxDecoration(
                            borderRadius: BorderRadius.circular(10.0),
                            border:
                                Border.all(color: Colors.black54, width: 1.0)),
                        child: Text(
                          '取消订单',
                          style:
                              TextStyle(fontSize: 14.0, color: Colors.black54),
                        ),
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: status != "6" && status != "4",
                    child: new GestureDetector(
                      onTap: () {
                        del();
                      },
                      child: new Container(
                        padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                        decoration: new BoxDecoration(
                            borderRadius: BorderRadius.circular(10.0),
                            border:
                                Border.all(color: Colors.black54, width: 1.0)),
                        child: Text(
                          '删除订单',
                          style:
                              TextStyle(fontSize: 14.0, color: Colors.black54),
                        ),
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: status != "2",
                    child: new GestureDetector(
                      onTap: () {
                        showDialog(
                          context: context,
                          builder: (context) => new AlertDialog(
                            title: new Text('确认提示'),
                            content: new Text('确定商家已提供服务吗，确认后不可撤回?'),
                            actions: <Widget>[
                              new FlatButton(
                                onPressed: () =>
                                    Navigator.of(context).pop(false),
                                child: new Text(
                                  '取消',
                                  style: TextStyle(color: Colors.grey),
                                ),
                              ),
                              new FlatButton(
                                onPressed: () {
                                  Navigator.of(context).pop(false);
                                  showDialog(
                                      context: context,
                                      barrierDismissible: false,
                                      builder: (_) {
                                        return new NetLoadingDialog(
                                          loadingText: "确认中...",
                                          bgColor: Color.fromARGB(255, 0, 0, 0),
                                          requestCallBack: confirm(),
                                          outsideDismiss: false,
                                        );
                                      });
                                },
                                child: new Text('确定完成',
                                    style: TextStyle(color: mainColor)),
                              ),
                            ],
                          ),
                        );
                      },
                      child: new Container(
                        padding: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setHeight(DP_20),
                            2,
                            ScreenUtil.getInstance().setHeight(DP_20),
                            2),
                        margin: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setHeight(DP_20), 0, 0, 0),
                        decoration: new BoxDecoration(
                            color: mainColor,
                            borderRadius: BorderRadius.circular(10.0),
                            border: Border.all(color: mainColor, width: 1.0)),
                        child: Text(
                          '确认完成',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE),
                              color: Colors.white),
                        ),
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: status != "4",
                    child: new GestureDetector(
                      onTap: () {
                        if (Platform.isAndroid) {
                          Navigator.of(context).push<String>(
                              MaterialPageRoute(builder: (context) {
                            return new LocalNavWebWidget(
                                Api.URL_Comment +
                                    'token=' +
                                    token +
                                    '&order_id=' +
                                    order_id,
                                "订单评价");
                          })).then((value) {
                            if (value != null) {
                              Toast.toast(context, msg: '评价成功');
                              Navigator.of(context).pop("1");
                            }
                          });
                          return;
                        }
                        Navigator.of(context)
                            .push<String>(MaterialPageRoute(builder: (context) {
                          return new OrderCommentPage(
                            url: Api.URL_Comment +
                                'token=' +
                                token +
                                '&order_id=' +
                                order_id,
                          );
                        })).then((value) {
                          if (value != null) {
                            Toast.toast(context, msg: '评价成功');
                            Navigator.of(context).pop("1");
                          }
                        });
                      },
                      child: new Container(
                        padding: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setHeight(DP_20),
                            2,
                            ScreenUtil.getInstance().setHeight(DP_20),
                            2),
                        margin: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setHeight(DP_20), 0, 0, 0),
                        decoration: new BoxDecoration(
                            color: mainColor,
                            borderRadius: BorderRadius.circular(10.0),
                            border: Border.all(color: mainColor, width: 1.0)),
                        child: Text(
                          '评价服务',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE),
                              color: Colors.white),
                        ),
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: status != "1",
                    child: new GestureDetector(
                      onTap: () {
                        Navigator.of(context).push(
                          new MaterialPageRoute(
                              builder: (context) => new OrderPayPage(
                                    orderId: order_id,
                                    orderNum: orderNum,
                                    discount: discount,
                                    totalMoney: pay_price,
                                  )),
                        );
                      },
                      child: new Container(
                        padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                        margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
                        decoration: new BoxDecoration(
                            color: mainColor,
                            borderRadius: BorderRadius.circular(10.0),
                            border: Border.all(color: mainColor, width: 1.0)),
                        child: Text(
                          '去支付',
                          style: TextStyle(fontSize: 14.0, color: Colors.white),
                        ),
                      ),
                    ),
                  ),
                  invoice == 1
                      ? Center()
                      : Offstage(
                          offstage: status != '4' && status != '5',
                          child: new GestureDetector(
                            onTap: () {
                              Navigator.of(context)
                                  .push<UserReceiptListDataData>(
                                      MaterialPageRoute(builder: (context) {
                                return ChooseReceiptPage();
                              })).then((UserReceiptListDataData data) {
                                print(data.purchaser);
                                print(data.invoice_id);
                                if (data == null) {
                                  return;
                                }
                                receipt_name = data.purchaser;
                                invoice_id = data.invoice_id;
                                //  coupon_name = coupon['money'].toString() + '元';
                                //  coupon_record_id = coupon['coupon_record_id'].toString();
                                //  coupon_money= coupon['money'].toString();
                                setState(() {});
                                showDialog(
                                  context: context,
                                  builder: (context) => new AlertDialog(
                                    title: new Text('确认提示'),
                                    content: new Text('确定选择该发票抬头吗，确认后不可撤回?'),
                                    actions: <Widget>[
                                      new FlatButton(
                                        onPressed: () =>
                                            Navigator.of(context).pop(false),
                                        child: new Text(
                                          '取消',
                                          style: TextStyle(color: Colors.grey),
                                        ),
                                      ),
                                      new FlatButton(
                                        onPressed: () {
                                          Navigator.of(context).pop(false);
                                          applyInvoice(widget.id);
                                        },
                                        child: new Text('确定完成',
                                            style: TextStyle(color: mainColor)),
                                      ),
                                    ],
                                  ),
                                );
                              });
                            },
                            child: new Container(
                              margin: EdgeInsets.fromLTRB(10, 4, 10, 4),
                              padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                              decoration: new BoxDecoration(
                                  borderRadius: BorderRadius.circular(10.0),
                                  border:
                                      Border.all(color: mainColor, width: 1.0)),
                              child: Text(
                                '申请开票',
                                style:
                                    TextStyle(fontSize: 14.0, color: mainColor),
                              ),
                            ),
                          ),
                        ),
                  // new GestureDetector(
                  //     onTap: () {
                  //       // if (invoice_id != null) {
                  //       //   print('申请发票');
                  //       //   applyInvoice(widget.id);
                  //       // } else {
                  //       //   //查看发票明细

                  //       // }
                  //     },
                  //     child: new Container(
                  //       margin: EdgeInsets.fromLTRB(10, 4, 10, 4),
                  //       padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                  //       decoration: new BoxDecoration(
                  //           borderRadius: BorderRadius.circular(10.0),
                  //           border: Border.all(color: mainColor, width: 1.0)),
                  //       child: Text(
                  //         invoice == 1 ? '查看发票' : '确定开票',
                  //         style: TextStyle(fontSize: 14.0, color: mainColor),
                  //       ),
                  //     ),
                  //   ),
                ],
              ),
            ),
            Container(
              height: 20,
            )
          ],
        ));
  }

  Widget _renderRow(BuildContext context, int index) {
    var price = RichText(
      textAlign: TextAlign.left,
      text: TextSpan(style: TextStyle(color: Colors.black), children: [
        TextSpan(
            text: '¥',
            style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(25))),
        TextSpan(
            text: detail[index]['price'],
            style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(25))),
        TextSpan(
            text: ' X' + detail[index]['num'].toString(),
            style: TextStyle(
              fontSize: 12.0,
              color: Colors.black,
            )),
      ]),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
    return new GestureDetector(
      child: Container(
        margin: EdgeInsets.fromLTRB(15, 10, 15, 5),
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            color: white,
            borderRadius: BorderRadius.circular(
                ScreenUtil.getInstance().setHeight(DP_10))),
        child: new Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            detail[index]['img'] == ""
                ? Image.asset('images/pic_default.png',
                    width: ScreenUtil.getInstance().setWidth(160),
                    fit: BoxFit.fill,
                    height: ScreenUtil.getInstance().setWidth(160))
                : Image.network(
                    Api.ImgBaseUrl + detail[index]['img'],
                    width: ScreenUtil.getInstance().setHeight(160),
                    height: ScreenUtil.getInstance().setHeight(160),
                    fit: BoxFit.fill,
                  ),
            Expanded(
              flex: 1,
              child: Container(
                padding: EdgeInsets.fromLTRB(10, 5, 5, 5),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      detail[index]['goods_name'],
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().setSp(25),
                          color: black,
                          fontWeight: FontWeight.w400),
                    ),
                    Row(
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: price,
                        ),
                        // Container(
                        //   alignment: Alignment.center,
                        //   width: 50,
                        //   child: Text('退换'),
                        // ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  confirm() async {
    Map map = new Map();
    map['token'] = token;
    map['id'] = widget.id;
    map['sign_type'] = createSignType(Api.URL_ORDER_CONFIRM);
    Response<String> response =
        await HttpUtil().post(Api.URL_ORDER_CONFIRM, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    // Fluttertoast.showToast(msg: msg);
    Toast.toast(context, msg: msg);
    if (code == 1) {
      getDetail();
    }
    return true;
  }

  del() async {
    // FormData formData = new FormData.fromMap({
    //   "token": token,
    //   "id": order_id,
    // });
    // Response<String> response =
    //     await HttpUtil().post(Api.DEL_ORDER, data: formData);
    // Map userMap = json.decode(response.data);
    // int code = userMap["code"];
    // String msg = userMap["msg"];
    // Fluttertoast.showToast(msg: msg);
    // if (code == 0) {
    //   Navigator.of(context).pop();
    // }
    // return true;
  }

  cancle() async {
    // FormData formData = new FormData.fromMap({
    //   "token": token,
    //   "id": order_id,
    // });
    Map map = new Map();
    map['token'] = token;
    map['id'] = widget.id;
    map['sign_type'] = createSignType(Api.URL_ORDER_CANCEL);
    Response<String> response =
        await HttpUtil().post(Api.URL_ORDER_CANCEL, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    // Fluttertoast.showToast(msg: msg);
    Toast.toast(context, msg: msg);
    if (code == 1) {
      getDetail();
    }
    return true;
  }

  getInvoiceMsg() async {
    //发票订单，判断是否有发票
    Map map = new Map();
    map['token'] = token;
    map['order_id'] = widget.id;
    map['sign_type'] = createSignType(Api.URL_ORDER_Invoice);
    print(map);
    Response<String> response =
        await HttpUtil().post(Api.URL_ORDER_Invoice, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    print(userMap);

    if (code == 1) {
      setState(() {
        receipt_content = '可查看';
        invoice = code;
        receipt_name = userMap['data']['invoiceMsg']['purchaser'];
        type_zh = userMap['data']['invoiceMsg']['type_zh'];
      });
    } else if (code == 205) {
      setState(() {
        invoice = code;
        receipt_name = '可选择发票抬头开票';
      });
    }
  }

  applyInvoice(String order_id) async {
    Map map = new Map();
    map['sign_type'] = createSignType(Api.URL_INVOICE_APPLY);
    map['invoice_id'] = invoice_id.toString();
    map['token'] = token;
    map['order_id'] = order_id.toString();
    print(map);
    Response<String> response =
        await HttpUtil().post(Api.URL_INVOICE_APPLY, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap['code'];
    print(userMap);
    if (code == 1) {
      getInvoiceMsg();
      // Fluttertoast.showToast(msg: '开票成功');
      Toast.toast(context, msg: '申请开票成功');
    }
  }
}
